package com.runfast.paotui.web.controller.server;

import com.runfast.common.utils.SessionContext;
import com.runfast.common.web.entity.Result;
import com.runfast.waimai.dao.model.BaseUser;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentaccounts;
import com.runfast.waimai.dao.model.WaiMaiRunfastAgentbusiness;
import com.runfast.waimai.dao.model.WaiMaiRunfastShopper;
import com.runfast.waimai.service.WaiMaiRunfastAgentbusinessService;
import com.runfast.waimai.service.WaiMaiRunfastShopperService;
import com.runfast.waimai.utils.TimeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Description:  跑腿骑手统计
 * @Auther: chenzheng
 * @Date: 2018/7/14 17:54
 **/
@RestController("shopperStatisticsController")
@RequestMapping("/server/paotui/shopperStatistics")
public class ShopperStatisticsController {
    @Autowired
    private WaiMaiRunfastShopperService waiMaiRunfastShopperService;
    @Autowired
    private WaiMaiRunfastAgentbusinessService runfastAgentbusinessService;
    /**
     * 骑手统计列表带分页的
     */
    @GetMapping("/list")
    public Result list(@PageableDefault Pageable pageable, String agentIds,String data) throws ParseException {
        Map<Object,Object> map1=new HashMap<Object, Object>();
        BaseUser admin = SessionContext.getAdmin();
        WaiMaiRunfastAgentaccounts agentAccounts= SessionContext.getCurrentRunfastAgentaccounts();
        if(admin!=null && admin.getType()==2 && agentAccounts!=null){
            //代理商
            if(StringUtils.isNotBlank(agentIds)){
                List<String> result = Arrays.asList(agentIds.split(","));
                if(!Integer.valueOf(result.get(0)).equals(agentAccounts.getAgentid())){
                    return Result.fail(null,"只能查看自己的配送信息！");
                }
            }
            map1.put("agentIds",agentAccounts.getAgentid());
        }else if(admin!=null && admin.getType()==1){
            //后台
            if(StringUtils.isNotBlank(agentIds)){
                map1.put("agentIds",agentIds);
            }
        }
        if(StringUtils.isNotBlank(data)){
            List<String> result = Arrays.asList(data.split(","));
            map1.put("startTime",TimeUtil.getDate(result.get(0)));
            map1.put("endTime",TimeUtil.getDate(result.get(1)));
        }
        map1.put("pageable",pageable);
        List<WaiMaiRunfastShopper> list=waiMaiRunfastShopperService.selectShopperStatisticsByMapWithPageable(map1);
        if(list.size()>0){
            for(WaiMaiRunfastShopper shopper:list){
                  if(shopper.getAgentid()!=null){
                      WaiMaiRunfastAgentbusiness runfastAgentbusiness=runfastAgentbusinessService.selectByPrimaryKey(shopper.getAgentid());
                      if(runfastAgentbusiness!=null){
                          shopper.setAgentname(runfastAgentbusiness.getName());
                      }
                      //else{return Result.fail(null," 找不到对应的代理商！");}
                  }
            }
        }
        Integer total=waiMaiRunfastShopperService.countShopperStatisticsByMap(map1);
        Map<Object,Object> map=new HashMap<Object, Object>();
        map.put("list",list);
        map.put("total",total);
        return Result.ok(null,map);
    }
}